﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UserInterfaceLayer.User
{
    public partial class RoomClean : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblFailure.Visible = false;
            lblInvalidInput.Visible = false;
            lblSuccessful.Visible = false;
            lblDeletedItem.Visible = false;
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            string strRoomID = Request.QueryString["RoomID"];
            DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
            int RoomID;
            if (!int.TryParse(strRoomID, out RoomID))
                Response.Redirect("~/");
            lvHistory.DataSource = BusinessLogicLayer.DataObject.RoomClean.Convert(DataAccessLayer.RoomClean.ToListByRoomID_IncludeDeleted(db, RoomID), DataAccessLayer.RequestRoom.ToListByRoomID_IncludeDeleted_Filter(db, RoomID, (int)BusinessLogicLayer.RequestRoomStatus.Finished));
            lvHistory.DataBind();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                int UserID = (int)Session["UserID"];
                string strRoomID = Request.QueryString["RoomID"];
                int RoomID = int.Parse(strRoomID);
                DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
                DataAccessLayer.RoomClean NewRoomClean = new DataAccessLayer.RoomClean();
                NewRoomClean.RoomID = RoomID;
                NewRoomClean.CreateUserID = UserID;
                NewRoomClean.CreateTimestamp = DateTime.Now;
                ToDatabase(NewRoomClean);
                db.RoomCleans.InsertOnSubmit(NewRoomClean);
                db.SubmitChanges();
                lblSuccessful.Visible = true;
            }
            catch (Exception)
            {
                lblFailure.Visible = true;
            }
        }

        public void FromDatabase(DataAccessLayer.RoomClean RoomClean)
        {
            txtDescription.Text = RoomClean.Description;
            ddlClean.SelectedValue = (RoomClean.IsClean ? "1" : "0");
            lblDeletedItem.Visible = RoomClean.Remove;
        }

        public void ToDatabase(DataAccessLayer.RoomClean RoomClean)
        {
            RoomClean.Description = txtDescription.Text;
            RoomClean.IsClean = (ddlClean.SelectedValue == "1");
        }
    }
}